package net.jiastudy.biz.datasync.strategy.pzxy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;

import net.jiastudy.biz.datasync.DataSyncStrategy;
import net.jiastudy.biz.datasync.strategy.pzxy.bean.DeptBean;
import net.jiastudy.biz.datasync.strategy.pzxy.bean.TeacherBean;
import net.jiastudy.biz.enums.DeptType;
import net.jiastudy.biz.enums.Gender;
import net.jiastudy.biz.sys.entity.Dept;
import net.jiastudy.biz.sys.service.DeptService;
import net.jiastudy.biz.user.entity.User;
import net.jiastudy.biz.user.service.UserService;
import net.jiastudy.core.exception.ServiceException;

@Component("pzxyDataSyncStrategy")
public class PzxyDataSyncStrategyImpl implements DataSyncStrategy {

	private JdbcTemplate jdbc;

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private DeptService deptService;

	@Resource
	UserService userService;

	@Override
	public boolean syncDepts() {
		String sql = "select * from bm_view";
		RowMapper<DeptBean> rowMapper = new BeanPropertyRowMapper<DeptBean>(DeptBean.class);
		List<DeptBean> beans = this.getJdbc().query(sql, rowMapper);
		for (DeptBean bean : beans) {
			try {
				Dept dept = new Dept();
				dept.setCode(bean.getBmdm());
				dept.setName(bean.getBmmc());
				dept.setType(DeptType.other);
				deptService.createDept(dept);
			} catch (ServiceException e) {
				logger.error(e.getMessage() + "名称为：" + bean.getBmmc() + "code为：" + bean.getBmdm());
			}
		}
		return true;
	}

	@Override
	public boolean syncTeacher() {
		String sql = "select * from jsxx_view";
		RowMapper<TeacherBean> rowMapper = new BeanPropertyRowMapper<TeacherBean>(TeacherBean.class);
		List<TeacherBean> beans = getJdbc().query(sql, rowMapper);
		List<Dept> depts = deptService.findAllDepts();
		Map<String, Dept> deptMap = new HashMap<>();
		for (Dept dept : depts) {
			deptMap.put(dept.getCode(), dept);
		}
		for (TeacherBean bean : beans) {
			try {

				User user = userService.getUserByUsername(bean.getJzgh());
				if (null == user) {
					user = new User();
					user.setUsername(bean.getJzgh());
					user.setPassword(bean.getJzgh());
					user.setName(bean.getXm());
					if (bean.getXb().equals("1")) {
						user.setGender(Gender.male);
					} else if (bean.getXb().equals("2")) {
						user.setGender(Gender.female);
					}

					if (deptMap.containsKey(bean.getBmbm())) {
						user.setDeptId(deptMap.get(bean.getBmbm()).getId());
					}
					userService.createUser(user);
				} else {
					user.setName(bean.getXm());
					if (bean.getXb().equals("1")) {
						user.setGender(Gender.male);
					} else if (bean.getXb().equals("2")) {
						user.setGender(Gender.female);
					}

					if (deptMap.containsKey(bean.getBmbm())) {
						user.setDeptId(deptMap.get(bean.getBmbm()).getId());
					}
					userService.updateUser(user);
				}

			} catch (ServiceException e) {
				logger.error(e.getMessage() + "姓名为：" + bean.getXm() + "工号为：" + bean.getJzgh());
			}
		}
		return true;
	}

	private JdbcTemplate getJdbc() {
		if (null != jdbc) {
			return jdbc;
		}
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl(
				"jdbc:mysql://10.10.62.82/pzxy?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai");
		dataSource.setUsername("pzxy_rs");
		dataSource.setPassword("Viewrs1264");
		jdbc = new JdbcTemplate(dataSource);
		return jdbc;
	}

}
